﻿using DLL.Standard.Infrastructure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Xml.Serialization;

namespace DLL.MS.Entity.Param.UpLoadDrug
{
    public class DicDrugConnectStr : CNotifyPropertyChanged
    {
        //服务器IP地址
        private string _ipAdress = "127.0.0.1";

        public string ipAdress
        {
            get { return _ipAdress; }
            set
            {
                if (_ipAdress != value)
                {
                    _ipAdress = value;
                    this.OnPropertyChanged();
                }
            }
        }

        //服务器端口号
        private string _serverPort = "3306";

        public string serverPort
        {
            get { return _serverPort; }
            set
            {
                if (_serverPort != value)
                {
                    _serverPort = value;
                    this.OnPropertyChanged();
                }
            }
        }

        //数据库名称
        private string _serverDataBaseName = "serversaveddrugs";

        public string serverDataBaseName
        {
            get { return _serverDataBaseName; }
            set
            {
                if (_serverDataBaseName != value)
                {
                    _serverDataBaseName = value;
                    this.OnPropertyChanged();
                }
            }
        }

        //登陆账户
        private string _logAccount = "root";

        public string logAccount
        {
            get { return _logAccount; }
            set
            {
                if (_logAccount != value)
                {
                    _logAccount = value;
                    this.OnPropertyChanged();
                }
            }
        }

        //登陆密码
        private string _logPassword = "123456";

        public string logPassword
        {
            get { return _logPassword; }
            set
            {
                if (_logPassword != value)
                {
                    _logPassword = value;
                    this.OnPropertyChanged();
                }
            }
        }

        //医嘱视图表名称
        private string _serverLableName = "drugs";

        public string serverLableName
        {
            get { return _serverLableName; }
            set
            {
                if (_serverLableName != value)
                {
                    _serverLableName = value;
                    this.OnPropertyChanged();
                }
            }
        }

        //数据库名称
        private string _localdataBaseName = "localsaveddrugs";

        public string localdataBaseName
        {
            get { return _localdataBaseName; }
            set
            {
                if (_localdataBaseName != value)
                {
                    _localdataBaseName = value;
                    this.OnPropertyChanged();
                }
            }
        }

        //药品字典视图表名称
        private string _localLableName = "drugs";

        public string localLableName
        {
            get { return _localLableName; }
            set
            {
                if (_localLableName != value)
                {
                    _localLableName = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private int _connectDrugFlag = 0;

        public int connectDrugFlag
        {
            get { return _connectDrugFlag; }
            set
            {
                if (_connectDrugFlag != value)
                {
                    _connectDrugFlag = value;
                    this.OnPropertyChanged();
                }
            }
        }
    }

    public static class ProcessConnectStr
    {
        public static bool Save(DicDrugConnectStr connectStr)
        {
            try
            {
                XmlSerializer xmlFormat = new XmlSerializer(typeof(DicDrugConnectStr));
                //现在将数据保存到本地的一个文件中
                string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
                string filePath = str_1 + @"Param\ConncetStr.xml";
                Stream fstream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
                xmlFormat.Serialize(fstream, connectStr);
                fstream.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public static bool Read(ref DicDrugConnectStr connectStr)
        {
            try
            {
                XmlSerializer xmlFormat1 = new XmlSerializer(typeof(DicDrugConnectStr));
                //string str2 = Application.StartupPath;
                string str_2 = System.AppDomain.CurrentDomain.BaseDirectory;
                string filePath2 = str_2 + @"Param\ConncetStr.xml";
                if (File.Exists(filePath2))
                {
                    Stream fstream = File.OpenRead(filePath2);
                    connectStr = (DicDrugConnectStr)xmlFormat1.Deserialize(fstream);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 采用Socket方式，测试服务器连接
        /// </summary>
        /// <param name="host">服务器主机名或IP</param>
        /// <param name="port">端口号</param>
        /// <param name="millisecondsTimeout">等待时间：毫秒</param>
        /// <returns></returns>
        public static bool TestConnection(string host, int port, int millisecondsTimeout)
        {
            TcpClient client = new TcpClient();
            try
            {
                var ar = client.BeginConnect(host, port, null, null);
                ar.AsyncWaitHandle.WaitOne(millisecondsTimeout);
                return client.Connected;
            }
            catch (Exception e)
            {
                //LogHelper.Error(e.Message);
                return false;                 //throw e;
            }
            finally
            {
                client.Close();
            }
        }
    }
}